/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.converter;

import com.giant.entities.TypeCurrency;
import com.giant.services.parametrics.ServiceTypeCurrencyLocal;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author Harold
 */
@ManagedBean
@RequestScoped
public class TypeCurrencyConverter implements Converter, Serializable {

    @EJB
    private ServiceTypeCurrencyLocal serviceTypeCurrency;

    public TypeCurrencyConverter() {
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value != null && !value.equals("")&& !value.equals("Select Type Currency")) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("idTypeCurrency", value);
            return serviceTypeCurrency.getTypeCurrencysFilter("TypeCurrency.findByIdTypeCurrency", map).get(0);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        return (value != null && !value.equals("")) ? ((TypeCurrency) value).getIdTypeCurrency().toString() : null;
    }
}
